package cn.jianyongjie.user.controller;

import cn.jianyongjie.basic.dto.VerificationCode;
import cn.jianyongjie.user.service.IUserService;
import cn.jianyongjie.user.domain.User;
import cn.jianyongjie.user.query.UserQuery;
import cn.jianyongjie.basic.utils.AjaxResult;
import cn.jianyongjie.basic.utils.QueryResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    public IUserService userService;



    /*
    * @Title: UserController
    * @Description: 手机注册
    * @Author: JianYongJie
    * @Version: 1.0
    * @Date:  2022/5/9 15:05
    * @Parameters: * @param
    * @Return cn.jianyongjie.basic.utils.AjaxResult
    */
    @PostMapping("/phone/register")
    public AjaxResult phoneRegister(@RequestBody VerificationCode code){
        try {
            userService.phoneRegister(code);
            return AjaxResult.success();
        } catch (ClassCastException e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }


    /*
     * @Title: UserController
     * @Description: 邮箱注册
     * @Author: JianYongJie
     * @Version: 1.0
     * @Date:  2022/5/9 15:05
     * @Parameters: * @param
     * @Return cn.jianyongjie.basic.utils.AjaxResult
     */
    @PostMapping("/mailbox/register")
    public AjaxResult mailboxRegister(@RequestBody VerificationCode mailbox){
        try {
            userService.mailboxRegister(mailbox);
            return AjaxResult.success();
        } catch (ClassCastException e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    /**
     * 保存和修改公用的
     * @param user  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody User user){
        try {
            if( user.getId()!=null)
                userService.update(user);
            else
                userService.save(user);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            userService.remove(id);
            return AjaxResult.success();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.error();
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public User get(@PathVariable("id")Long id)
    {
        return userService.loadById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<User> list(){

        return userService.loadAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return QueryResult 分页对象
    */
    @PostMapping("/list")
    public QueryResult<User> json(@RequestBody UserQuery query)
    {
        return userService.queryData(query);
    }
}
